package com.cat.dataStructure02;

class Bank {
    long[] balance;
    public Bank(long[] balance) {
        this.balance = balance;
    }
    boolean check(int n) {
        if (n < 0 || n >= balance.length) {
            return false;
        }
        return true;
    }
    public boolean transfer(int account1, int account2, long money) {
        int a = account1 - 1, b = account2 - 1;
        if (!check(a) || !check(b)) {
            return false;
        }
        if (balance[a] >= money) {
            balance[a] -= money;
            balance[b] += money;
            return true;
        }
        return false;
    }

    public boolean deposit(int account, long money) {
        if (!check(account - 1)) {
            return false;
        }
        balance[account - 1] += money;
        return true;
    }

    public boolean withdraw(int account, long money) {
        if (!check(account - 1)) {
            return false;
        }
        if (balance[account - 1] >= money) {
            balance[account - 1] -= money;
            return true;
        }
        return false;
    }
}